package com.victualler.core.config.dict.repository.impl;

import com.victualler.core.base.repository.impl.BaseQueryRepositoryImpl;
import com.victualler.core.common.utils.ExceptionWapperUtil;
import com.victualler.core.common.utils.HQLConverterUtil;
import com.victualler.core.config.dict.dto.DictTopDto;
import com.victualler.core.config.dict.entity.DictDao;
import com.victualler.core.config.dict.repository.DictQueryRepository;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Aouthor Guoshichang
 * @Date 9:34 on 2018/6/27.
 * @Function 封装用于复杂sql查询的API. 为每个实体的repository层提供便利操作
 */
@Component
public class DictQueryRepositoryImpl extends BaseQueryRepositoryImpl<DictDao,Long> implements DictQueryRepository {

    /**
     * @author Guoshichang
     * @since 20:26 on 2018/8/27.
     * @param codeType 字典类型
     * @return List
     * @Function 根据codeType 模糊查询
     */
    public List<DictTopDto> getDictTopDtoList(String codeType) {
        String sql = "select code_type,type_name,count(id) from "+this.getTableName(DictDao.class)+" where code_type like ?   group by code_type,type_name";
        try {
           return HQLConverterUtil.findVOListBySql(sql,new Object[]{"%"+codeType+"%"},DictTopDto.class,this.entityManager);
        } catch (Exception e) {
            throw ExceptionWapperUtil.wapper(e);
        }
    }
}
